﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace Tzx.TUpdater.Utilities
{
    /// <summary>
    /// 进程信息帮助类
    /// <![CDATA[
    /// Process pCurrent = Process.GetCurrentProcess();
    /// Process parent = ProcessHelper.GetParent(pCurrent);
    /// textBox1.Text = "ProcessName=" + parent.ProcessName + "\r\n\r\n";
    /// textBox1.Text += ",Id=" + parent.Id + "\r\n\r\n";
    /// textBox1.Text += ",Xml_Path=" + parent.MainModule.Xml_Path + "\r\n\r\n";
    /// ]]>
    /// </summary>
    internal class ProcessHelper
    {
        #region 获得父进程

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        private static string FindIndexedProcessName(int pid)
        {
            string processName = Process.GetProcessById(pid).ProcessName;
            Process[] processesByName = Process.GetProcessesByName(processName);
            string processIndexdName = null;
            for (int index = 0; index < processesByName.Length; index++)
            {
                processIndexdName = index == 0 ? processName : processName + "#" + index;
                PerformanceCounter processId = new PerformanceCounter("Process", "ID Process", processIndexdName);
                if ((int)processId.NextValue() == pid)
                {
                    return processIndexdName;
                }
            }
            return processIndexdName;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="indexedProcessName"></param>
        /// <returns></returns>
        private static Process FindPidFromIndexedProcessName(string indexedProcessName)
        {
            //效率欠佳
            PerformanceCounter parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName);
            return Process.GetProcessById((int)parentId.NextValue());
        }

        /// <summary>
        /// 获得父进程
        /// </summary>
        /// <param name="process"></param>
        /// <returns></returns>
        internal static Process GetParent(Process process)
        {
            if (process != null)
            {
                string processName = FindIndexedProcessName(process.Id);
                return FindPidFromIndexedProcessName(processName);
            }
            return null;
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="exePathNameSs"></param>
        /// <returns></returns>
        internal int KillProcess(ICollection<string> exePathNameSs)
        {
            Process[] allProcess = Process.GetProcesses();
            foreach (Process p in allProcess)
            {
                foreach (string exe in exePathNameSs)
                {
                    //if (p.ProcessName.ToLower() + ".exe" == exe.ToLower() && p.MainModule.FileName.ToLower().CompareTo(exe.ToLower())==0)
                    if (
                        String.Compare(string.Format("{0}.exe", p.ProcessName), exe,StringComparison.InvariantCultureIgnoreCase) == 0
                        &&
                        String.Compare(p.MainModule.FileName, exe,StringComparison.InvariantCultureIgnoreCase) == 0
                        )
                    {
                        for (int i = 0; i < p.Threads.Count; i++)
                        {
                            p.Threads[i].Dispose();
                        }
                        p.Kill();
                        //isRun = true;
                        //break;
                    }
                }//foreach
            }//foreach

            throw new NotImplementedException("上有老，下有小，没太多时间做了。");
        }

    }
}